package heven.holt.wan.android.viewmodel

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import heven.holt.wan.android.repository.WanProjectRepository
import javax.inject.Inject

/**
 * important!!!
 * 当flow在ViewModel中必须为成员变量时（Fragment加入回退栈不重新初始化变量），
 * 此种方案利用savedStateHandle自动获取Fragment的arguments
 * 另一种方案为手动传输cid到ViewModel初始化对象，这种方案必须实现ViewModelProvider.Factory
 * @see SystemArticleViewModel
 */
@HiltViewModel
class ProjectArticleViewModel @Inject constructor(
    wanProjectRepository: WanProjectRepository,
    savedStateHandle: SavedStateHandle
) : ViewModel() {
    private val cid: Int = savedStateHandle.get<Int>("cid")!!

    val flow = wanProjectRepository.getArticleProjectListFlow(cid).cachedIn(viewModelScope)
}